<?php
namespace structure\components;

class Template extends \components\Component
{
	//Duplicate just to iterate with next
	private $children = array();

	public function Start()
	{
		$this->children = $this->GetChildren();

		$file = _COMPONENTS_DIR."templates/".$this->GetComponent()->GetAttribute('name').".template.html";

		$content = core("File")->Read($file);

		$res = preg_replace_callback('~<!\-\-\{(.*)\}\-\->~iU', array($this, "ReplacePlaceholder"), $content);

		$form = $this->GetForm();

		if(!empty($form))
		{
			//WTF!! This sh** has just broken of a sudden!
			//					$this->CreateElement("form", $res);
			//$this->AddAttribute("id", $form);
		}

		return $res;
	}

	private function ReplacePlaceholder($arr)
	{
		$res = current($this->children);

		if (is_object($res))
		{
			$res = $res->Build();
		}

		next($this->children);
		return $res;
	}
}